package com.zhu.library_management_system.service.Impl;

import com.zhu.library_management_system.pojo.User;
import com.zhu.library_management_system.repository.UserRepository;
import com.zhu.library_management_system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
   private UserRepository userRepository;
    @Override
    public User login(String name, Integer password) {
        return userRepository.Login(name,password);
    }

    @Override
    public Iterable<User> SelectUserAll() {
        return userRepository.findAll();
    }

    @Override
    public void UserAdd(User user) {
        userRepository.save(user);
    }

    @Override
    public User register(String name) {
        return userRepository.register(name);
    }

    @Override
    public void UserDelete(Integer userId) {
        userRepository.deleteById(userId);
    }

    @Override
    public User SelectUser(Integer userId) {
        return userRepository.findById(userId).
                orElseThrow(() -> new RuntimeException("用户不存在: " + userId));
    }

    @Override
    public void UserUpdate(User user) {
        userRepository.save(user);
    }

    @Override
    public User getRole(Integer userId) {
        return userRepository.findById(userId).
                orElseThrow(() -> new RuntimeException("用户不存在: " + userId));
    }
}
